﻿package edu.bupt.segmenter;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.fudan.nlp.cn.tag.CWSTagger;

public class ChSplitWord {
	
	//停用词列表
	private Set<String> stopWords = new HashSet<String>();
	
	//FundanNPL版本为1.56
	private CWSTagger tag;
	
	private volatile static ChSplitWord instance;
	public static ChSplitWord getInstance(){
		if(instance==null){
			synchronized (ChSplitWord.class) {
				if(instance==null){
					try {
						instance = new ChSplitWord();
						//instance.readStopWords("./swlist.txt");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return instance;
		
	}
	
	public ChSplitWord() throws Exception{
		tag = new CWSTagger("./models/seg.m");
		tag.setEnFilter(true);
	}
	
	/*进行中文分词
	*输入文本内容content
	*输出分词结果result*/
	public String splitWord(String content) throws Exception{
		
		//使用FundanNLP进行分词
		String tagResult = tag.tag(content);		
		
		String[] words = tagResult.split(" ");
		String result =  "";
		Pattern p = Pattern.compile("[\\pS\\pN\\pP\\pC]*");
		Matcher m;
		
		for(int i = 0; i < words.length; i++){
			
			//去除中文单词前后的标点符号
			if(words[i].length() > 1 && (words[i].matches("\\pP[\\u4e00-\\u9fa5]+") || words[i].matches("[\\u4e00-\\u9fa5]+\\pP"))){
				words[i] = words[i].replaceAll("\\pP", "");
			}
			
			m = p.matcher(words[i]);     
			
			//去停用词，过滤数字符号等组成的单词
			if(!stopWords.contains(words[i]) && !m.matches()){
				result += words[i] + " ";				
			}
			
		}
		
		return result;
	}	
	
	/*从文件读取停用词表
	*输入停用词表文件路径swlistPath
	*输出停用词列表stopWords*/
	public Set<String> readStopWords(String swlistPath) throws IOException{
		File file = new File(swlistPath);
		InputStream in = new FileInputStream(file);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String line;
				
		while((line = br.readLine()) != null){
			stopWords.add(line);
			}
		
		return stopWords;
	}
	
	public static void main(String args[]){
		try {
			new ChSplitWord().getInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
